package com.bupt.liutong.admin.shop.bo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.base.FileUploader;
import com.bupt.liutong.admin.shop.dao.AttrValDao;
import com.bupt.liutong.admin.shop.dao.ProdDao;
import com.bupt.liutong.admin.shop.dao.ProdPhotoDao;
import com.bupt.liutong.admin.shop.dto.Cart;
import com.bupt.liutong.admin.shop.dto.Prod;
import com.bupt.liutong.admin.shop.form.CartForm;
import com.bupt.liutong.admin.shop.form.ProdForm;
import com.bupt.liutong.util.BeanUtils;

public class ProdBo extends BaseBo {
	
	private ProdDao prodDao;
	private ProdPhotoDao prodPhotoDao;
	private AttrValDao attrValDao;
	private FileUploader fileUploader;
	
	public List<ProdForm> getProdsByPage(ProdForm prodForm, HttpServletRequest request){
		Prod prod = new Prod();
		sm.pagingWithNoCriteria(prodForm, prod, "shop_prod.getProdsCount");
		prod.setUnTopTypeId(CourseBo.COURSE_ID);	// 商品类型是除课程之外的
		List<Prod> prods = prodDao.getProdsByPage(prod);
		return BeanUtils.dtoToForm(prods, ProdForm.class);
	}
	
	public ProdForm getProdById(ProdForm prodForm){
		ProdForm resultForm = new ProdForm();
		Prod prod = prodDao.getProdById(prodForm.getId());
		BeanUtils.copyProperties(resultForm, prod);
		return resultForm;
	}
	
	@Transactional
	public void insertProd(ProdForm prodForm, HttpServletRequest request) {
		Prod prod = new Prod();
		BeanUtils.copyProperties(prod, prodForm);
		Integer prodId = prodDao.insertProd(prod);
		prodForm.setId(prodId);
		fileUploader.uploadImages(prodForm, prodPhotoDao, request);
	}

	@Transactional
	public void updateProd(ProdForm prodForm, HttpServletRequest request) {
		Prod prod = new Prod();
		BeanUtils.copyProperties(prod, prodForm);
		prodDao.updateProd(prod);
		fileUploader.updateImages(prodForm, prodPhotoDao, request);
	}
	
	@Transactional
	public void deleteProds(String[] ids) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				Integer prodId = Integer.parseInt(ids[i]);
				prodPhotoDao.deleteProdPhotoByProdId(prodId);
				attrValDao.deleteAttrValByProdId(prodId);
				prodDao.deleteProd(prodId);
			}
		}
	}
	
	public ProdDao getProdDao() {
		return prodDao;
	}

	public void setProdDao(ProdDao prodDao) {
		this.prodDao = prodDao;
	}

	public void setProdPhotoDao(ProdPhotoDao prodPhotoDao) {
		this.prodPhotoDao = prodPhotoDao;
	}

	public void setAttrValDao(AttrValDao attrValDao) {
		this.attrValDao = attrValDao;
	}

	public void setFileUploader(FileUploader fileUploader) {
		this.fileUploader = fileUploader;
	}
}
